package com.daxia.bumall.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.daxia.bumall.daobase.GenericDAOHibernate;
import com.daxia.bumall.dto.GzActivityDTO;
import com.daxia.bumall.model.GzActivity;
import com.daxia.bumall.support.Page;

@Repository
public class GzActivityDAO extends GenericDAOHibernate<GzActivity> {

    public List<GzActivity> find(GzActivityDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From GzActivity n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (StringUtils.isNotBlank(dto.getMobile())) {
            hql.append(" and n.mobile like ? ");
            paras.add("%" + dto.getMobile() + "%");
        }					
        if (StringUtils.isNotBlank(dto.getName())) {
            hql.append(" and n.name like ? ");
            paras.add("%" + dto.getName() + "%");
        }					
        hql.append(" order by n.id desc ");
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public GzActivity findOne(GzActivityDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<GzActivity> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

	public int count() {
		String hql = "select count(*) from GzActivity";
		return super.countByHql(hql, null);
	}
}
